package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.WeakPwdMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.Hotfix;
import com.tpp.threat_perception_platform.pojo.WeakPwd;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.WeakPwdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class WeakPwdServiceImpl implements WeakPwdService {

    @Autowired
    private WeakPwdMapper weakPwdMapper;
    @Override
    public int saveWeakPwd(WeakPwd weakPwd) {
        int count = weakPwdMapper.countByMacAddress(weakPwd.getMacAddress());
        // 设置公共属性
        weakPwd.setDetectTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return weakPwdMapper.insertSelective(weakPwd);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        weakPwdMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult weakPwdList(MyParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        List<WeakPwd> dataList;

        if (param.getMacAddress() != null && !param.getMacAddress().isEmpty()) {
            // 按MAC地址查询
            dataList = weakPwdMapper.findByMacAddress(param);
        } else if (param.getHostName() != null && !param.getHostName().isEmpty()) {
            // 按主机名查询
            dataList = weakPwdMapper.findByHostName(param);
        } else if (param.getAccountName() != null && !param.getAccountName().isEmpty()) {
            // 按账户名查询
            dataList = weakPwdMapper.findByAccountName(param);
        } else if (param.getWeakPwd() != null && !param.getWeakPwd().isEmpty()) {
            // 按弱密码查询
            dataList = weakPwdMapper.findByWeakPwd(param);
        } else {
            // 查询所有
            dataList = weakPwdMapper.findAll(param);
        }

        PageInfo<WeakPwd> pageInfo = new PageInfo<>(dataList);
        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

}
